package com.sunlands.deskmate.message.handler.dispatcher.impl;

import com.sunlands.deskmate.message.entity.Messages;
import com.sunlands.deskmate.message.handler.dispatcher.Dispatcher;
import com.sunlands.deskmate.utils.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.Objects;

/**
 * 只发送给toId的人离线消息
 */
@Slf4j
@Component
public class DynamicMessageDispatcher implements Dispatcher {

    @Autowired
    private GroupMessageDispatcher groupMessageDispatcher;

    @Override
    public void dispatcher(Messages messages) {

        Long oldAckId = messages.getAckId();
        messages.setAckId(IdGenerator.id());
        log.info("1.2 防止消息乱序，将消息进行转换 重新生成ackId oldAckId = {} newAckId = {}", oldAckId, messages.getAckId());

        if(Objects.isNull(messages.getRealToId())){
            messages.setRealToId(Long.parseLong(messages.getToId()));
        }

        if (Objects.nonNull(messages.getRealToId())) {
//            messages.setAckId(IdGenerator.id());
            groupMessageDispatcher.sendHttpMessage(Collections.singletonList(messages.getRealToId()), messages);
        }

    }
}
